package com.frame.work.rabbitmq;

import com.frame.work.common.constant.Const;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.entity.GatewayLogMessageDO;
import com.frame.work.mapper.TblGatewayMapper;
import com.frame.work.service.TblMessageService;
import com.frame.work.service.impl.JpushService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * @author wjming
 * @ClassName: GeneralQueueProcessing
 * @Description:网关相关的通用队列处理
 *   //@RabbitListener(queues = "${topic.rabbit.gatewaySocketInvalid}")
 * @Date 2021/2/2 20:13
 */
@Component
public class GatewayGeneralQueueProcessing {

    @Autowired
    private TblGatewayMapper gatewayMapper;
    @Autowired
    private TblMessageService tblMessageService;
    @Autowired
    private JpushService jPushService;

    /**
     * @Description:数据帧处理通用数据处理
     * @param: getMap
     * @return: void
     * @author:wjming
     * @Date:2021/2/2 20:16
     */
    @RabbitHandler
    public void GeneralDeal(Map<String, Object> getMap) {
        String type = (String) getMap.get("dealType");
        try {
            switch (type) {
                //socket异常推送
                case "socketInvalid":
                    GatewayLogMessageDO gatewayLogMessageDO = gatewayMapper.getUserMessageByGatewayMac((String) getMap.get("gatewayMac"));
                    if (gatewayLogMessageDO != null) {
                        String isTrue = "1";
                        boolean isPush = StringUtils.equals(gatewayLogMessageDO.getGatewayPush(), isTrue);

                        jPushService.send("", gatewayLogMessageDO.getHomeName() + "的网关与服务器连接不稳定，请检查网络或供电是否正常", TypeEnum.DEVICE_TYPE.code(),
                                gatewayLogMessageDO.getSourceId(),
                                Const.MsgType.DEVICEORDINARY, null, gatewayLogMessageDO.getUserId(), gatewayLogMessageDO.getSourceType(),
                                gatewayLogMessageDO.getOrganizationCode(), isPush);
                    }
                    break;
                default:
                    break;

            }
        } catch (Exception e) {
            System.out.println("e = " + e);
        }

    }
}
